<style lang="stylus" scoped>
.top
  background linear-gradient(to right, #04befe , #4481eb)
  height 22rem
  padding 5rem
  text-align center
  color #fff
  line-height 1.8
  font-size 1.6rem
  .icon-head
    width 9rem
    height 9rem
    border-radius 50%
    background #ffffff
    background-image url('./imgs/head_03.png')
    background-size 9rem 9rem
    margin 0rem auto 1rem auto
    font-size 1.2rem
    .emblem
      position absolute
      display inline-block
      background #fdb72f
      margin 7.5rem .7rem
      padding 0px 5px
      border-radius 1.2rem
  a
    color #ffffff
  .stitle
    font-size 1.4rem
.btn
  display inline-block
  width 90%
  margin 2rem 5%
  font-size 1.6rem
  background linear-gradient(to right,#04befe, #4481eb)
</style>
<template>
  <div>
    <div class="top">
      <div class="icon-head" v-if="this.$store.state.user">
        <span v-if="this.$store.state.user.level>0" class="emblem">合伙人</span>
      </div>
        <p v-if="!this.$store.state.show">
          <router-link to="/Login">登录</router-link>/<router-link to="/Register">注册</router-link>
        </p>
        <p v-if="this.$store.state.show">
          姓名<span v-if="this.$store.state.user">{{this.$store.state.user.username}}</span>
        </p>
        <p v-if="!this.$store.state.show" class="stitle" @click="handleClick">登录注册有好礼相送~~</p>
        <p v-if="this.$store.state.show" class="stitle" @click="handleClick">
          <span v-if="this.$store.state.user">
            ID:{{this.$store.state.user.userId}}({{this.$store.state.user.phone}})
          </span>
        </p>
    </div>
    <LoginList></LoginList>
    <Button class="btn" type="primary" shape="circle" v-if="this.$store.state.show"  @click="handleExit">退出登录</Button>
  </div>
</template>
<script>
import axios from 'axios'
import LoginList from '@/components/LoginList'
export default {
  name: 'Login',
  components:{
    LoginList
  },
  mounted () {
    console.log('tag==', this.$route.query)
    console.log('tag', this.$store.state)
    // 如果当前是登录状态，则更新一下user对象
    // 更新一下
    if(this.$store.state.user.openId){
      axios.post('/web/api/manager/selectUser',{"userId":this.$store.state.user.userId,"openId":this.$store.state.user.openId}).then(this.selectSubordinateSucc)
    }
    if(this.$route.query.openid){
        this.openId = this.$route.query.openid;
        let userId =this.$route.query.userId;
        axios.post('/web/api/manager/selectUser',{"userId":userId,"openId":this.$route.query.openid}).then(this.selectSubordinateSucc)
    }
  },
  data () {
    return{
      isLogin:false,
      openId:"",
    }
  },
  methods:{
    selectSubordinateSucc(res){
      var data = res.data
      var user = res.data.date
      this.$Message.success(data.message);
      if(data.code==1){
        this.$store.state.user = user;
        console.log('存储的user', this.$store.state.user)
        if(this.openId){
          this.$store.state.user.openId =this.openId;
        }
        this.$store.state.show = true;
        localStorage.setItem("store",JSON.stringify(this.$store.state))
        this.$router.push({path: '/'});
      }else{
        return
      }
      console.log(res)
    },
    handleClick () {
      console.log('user========',  this.$store.state.user)
       console.log('state========',  this.$store.state)

    },
    handleExit () {
      this.$store.state.show = false
      this.$store.state.user={}
      localStorage.setItem("store",JSON.stringify(this.$store.state))
    }
  }
}
</script>


